# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libgme
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.6.3
pkgrel=3
pkgdesc="Video game music file emulation/playback library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://bitbucket.org/mpyne/game-music-emu/wiki/Home"
license=('LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
options=('strip' '!emptydirs' 'staticlibs')
source=("https://bitbucket.org/mpyne/game-music-emu/downloads/game-music-emu-${pkgver}.tar.xz"
         game-music-emu_86a449eec09d_013d4676c689.patch
         game-music-emu_414e0d993548_22e5c689f33f.patch
         game-music-emu_b3d158a30492_414e0d993548.patch)
sha256sums=('aba34e53ef0ec6a34b58b84e28bf8cfbccee6585cebca25333604c35db3e051d'
            '251f0e8035563862e563267572a6ecb76f953273b1e6f43ca1f2587df445d165'
            '0e4b5efdfb4ea40bf7939b99cc3f51f2c7cbb62b651e642a3d1ab352a0b69469'
            '9de71c0ab67feb7a3cf98b5d7b482bd435f960387c5d9fed4807494219b199a1')

prepare() {
  cd "${srcdir}/game-music-emu-${pkgver}"
  patch -Np1 -i ../game-music-emu_86a449eec09d_013d4676c689.patch
  patch -Np1 -i ../game-music-emu_414e0d993548_22e5c689f33f.patch
  patch -Np1 -i ../game-music-emu_b3d158a30492_414e0d993548.patch
}

build() {
  echo "Building static library"
  [[ -d "${srcdir}/build-${CARCH}-static" ]] && rm -rf "${srcdir}/build-${CARCH}-static"
  mkdir -p "${srcdir}/build-${CARCH}-static" && cd "${srcdir}/build-${CARCH}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DENABLE_UBSAN=OFF \
    -DBUILD_SHARED_LIBS=OFF \
    ../game-music-emu-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./

  echo "Building shared library"
  [[ -d "${srcdir}/build-${CARCH}-shared" ]] && rm -rf "${srcdir}/build-${CARCH}-shared"
  mkdir -p "${srcdir}/build-${CARCH}-shared" && cd "${srcdir}/build-${CARCH}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DENABLE_UBSAN=OFF \
    -DBUILD_SHARED_LIBS=ON \
    ../game-music-emu-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${CARCH}-static"
   DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  cd "${srcdir}/build-${CARCH}-shared"
   DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  install -Dm644 "${srcdir}/game-music-emu-${pkgver}/license.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/license.txt"
}
